package com.lanier.dydownloader;

import java.io.BufferedReader;
import java.io.InputStreamReader;  
import java.net.HttpURLConnection;  
import java.net.URL;
import java.util.List;
import java.util.Map;
  
public class HttpURLConnectionExample {  
    public static void main(String[] args) {  
        try {  
            // 创建URL对象并打开连接  
            URL url = new URL("https://example.com");  
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();  
  
            // 设置请求方法和请求头信息  
            connection.setRequestMethod("GET");  
            connection.setRequestProperty("Cookie", "your_cookie_value");  
  
            // 发送请求并获取响应  
            int responseCode = connection.getResponseCode();  
            System.out.println("Response Code: " + responseCode);  
  
            // 读取响应的Cookie信息  
            Map<String, List<String>> headers = connection.getHeaderFields();
            for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
                if (entry.getKey().equalsIgnoreCase("Set-Cookie")) {  
                    System.out.println("Cookie: " + entry.getValue());  
                }  
            }  
  
            // 读取响应内容  
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));  
            String line;  
            while ((line = reader.readLine()) != null) {  
                System.out.println(line);  
            }  
            reader.close();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
}